#!/usr/bin/perl

use EBox::Ldap;
use TryCatch;

sub baseDn {
    my ($ldap) = @_;

    my $result = $ldap->search(
        'base' => '',
        'scope' => 'base',
        'filter' => '(objectclass=*)',
        'attrs' => ['namingContexts']
    );
    my $entry = ($result->entries)[0];
    my $attr = ($entry->attributes)[0];
    return $entry->get_value($attr);
}

my $ldap;
try {
    $ldap = EBox::Ldap->safeConnect('127.0.0.1');
} catch (Error $e) {
    exit 1;
}

my $rootdn = 'cn=admin,' . baseDn($ldap);
my $rootpass = $ARGV[0];

my $res = $ldap->bind($rootdn, password => $rootpass);
if ($res->{resultCode} eq 0) {
    exit 0;
} else {
    exit 1;
}
